<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>卖家API调试</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .test-section { margin: 20px 0; padding: 15px; border: 1px solid #ddd; }
        .success { color: green; }
        .error { color: red; }
        .info { color: blue; }
        button { padding: 10px 20px; margin: 5px; }
        pre { background: #f5f5f5; padding: 10px; overflow-x: auto; }
    </style>
</head>
<body>
    <h1>卖家API调试页面</h1>
    
    <div class="test-section">
        <h3>1. 测试API基础连接</h3>
        <button onclick="testBasicAPI()">测试API连接</button>
        <div id="basicResult"></div>
    </div>

    <div class="test-section">
        <h3>2. 测试卖家店铺API</h3>
        <button onclick="testSellerShop()">获取店铺信息</button>
        <div id="shopResult"></div>
    </div>

    <div class="test-section">
        <h3>3. 测试卖家商品API</h3>
        <button onclick="testSellerProducts()">获取商品列表</button>
        <div id="productsResult"></div>
    </div>

    <div class="test-section">
        <h3>4. 测试卖家统计API</h3>
        <button onclick="testSellerStats()">获取统计数据</button>
        <div id="statsResult"></div>
    </div>

    <div class="test-section">
        <h3>5. 测试添加商品API</h3>
        <button onclick="testAddProduct()">添加测试商品</button>
        <div id="addProductResult"></div>
    </div>

    <script src="js/api.js"></script>
    <script>
        const username = 'seller001'; // 使用已知的卖家账户

        async function testBasicAPI() {
            const result = document.getElementById('basicResult');
            try {
                const response = await fetch('http://localhost:8000/api/products');
                const data = await response.json();
                result.innerHTML = `<div class="success">✓ API连接正常</div><pre>${JSON.stringify(data, null, 2)}</pre>`;
            } catch (error) {
                result.innerHTML = `<div class="error">✗ API连接失败: ${error.message}</div>`;
            }
        }

        async function testSellerShop() {
            const result = document.getElementById('shopResult');
            try {
                console.log('测试卖家店铺API...');
                const response = await fetch(`http://localhost:8000/api/seller/shop?username=${username}`);
                console.log('响应状态:', response.status);
                const data = await response.json();
                console.log('响应数据:', data);
                
                if (data.success) {
                    result.innerHTML = `<div class="success">✓ 店铺信息获取成功</div><pre>${JSON.stringify(data, null, 2)}</pre>`;
                } else {
                    result.innerHTML = `<div class="error">✗ 获取店铺信息失败: ${data.error}</div>`;
                }
            } catch (error) {
                result.innerHTML = `<div class="error">✗ 获取店铺信息失败: ${error.message}</div>`;
            }
        }

        async function testSellerProducts() {
            const result = document.getElementById('productsResult');
            try {
                console.log('测试卖家商品API...');
                const response = await fetch(`http://localhost:8000/api/seller/products?username=${username}`);
                console.log('响应状态:', response.status);
                const data = await response.json();
                console.log('响应数据:', data);
                
                if (data.success) {
                    result.innerHTML = `<div class="success">✓ 商品列表获取成功</div><pre>${JSON.stringify(data, null, 2)}</pre>`;
                } else {
                    result.innerHTML = `<div class="error">✗ 获取商品列表失败: ${data.error}</div>`;
                }
            } catch (error) {
                result.innerHTML = `<div class="error">✗ 获取商品列表失败: ${error.message}</div>`;
            }
        }

        async function testSellerStats() {
            const result = document.getElementById('statsResult');
            try {
                console.log('测试卖家统计API...');
                const response = await fetch(`http://localhost:8000/api/seller/stats?username=${username}`);
                console.log('响应状态:', response.status);
                const data = await response.json();
                console.log('响应数据:', data);
                
                if (data.success) {
                    result.innerHTML = `<div class="success">✓ 统计数据获取成功</div><pre>${JSON.stringify(data, null, 2)}</pre>`;
                } else {
                    result.innerHTML = `<div class="error">✗ 获取统计数据失败: ${data.error}</div>`;
                }
            } catch (error) {
                result.innerHTML = `<div class="error">✗ 获取统计数据失败: ${error.message}</div>`;
            }
        }

        async function testAddProduct() {
            const result = document.getElementById('addProductResult');
            try {
                console.log('测试添加商品API...');
                const productData = {
                    username: username,
                    name: '调试测试商品',
                    price: 99.99,
                    category: 'Food',
                    location: '测试产地',
                    stock: 10,
                    description: '这是一个调试测试商品'
                };
                
                const response = await fetch('http://localhost:8000/api/seller/products', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(productData)
                });
                console.log('响应状态:', response.status);
                const data = await response.json();
                console.log('响应数据:', data);
                
                if (data.success) {
                    result.innerHTML = `<div class="success">✓ 商品添加成功</div><pre>${JSON.stringify(data, null, 2)}</pre>`;
                } else {
                    result.innerHTML = `<div class="error">✗ 添加商品失败: ${data.error}</div>`;
                }
            } catch (error) {
                result.innerHTML = `<div class="error">✗ 添加商品失败: ${error.message}</div>`;
            }
        }

        // 页面加载时自动测试基础连接
        window.addEventListener('load', () => {
            testBasicAPI();
        });
    </script>
</body>
</html>
